package com.xiaoshou.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.xiaoshou.service.AnswersService;
import com.xiaoshou.service.ClientService;
import com.xiaoshou.service.FilesService;
import com.xiaoshou.service.LecturesService;
import com.xiaoshou.service.QuestionService;
import com.xiaoshou.service.QuestionTypeService;
import com.xiaoshou.service.UserService;

public abstract class BaseAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;

	public static final String LIST = "list";

	private Map<String, Object> session;
	private QuestionService questionService;
	private FilesService filesService;
	private UserService userService;
	private AnswersService answersService;
	private ClientService clientService;
	private QuestionTypeService questionTypeService;
	
	private LecturesService lecturesService;

	@Override
	public abstract String execute() throws Exception;

	/**
	 * 返回的是用户权限字符串
	 * @param user
	 * @return
	 */
	public void sendJson(Object o) {
		Gson gson = new Gson();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(gson.toJson(o));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void putInSession(String key, Object value) {
		session.put(key, value);
	}

	public Object getFromSession(String key) {
		return session.get(key);
	}

	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	@Autowired
	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public FilesService getFilesService() {
		return filesService;
	}

	@Autowired
	public void setFilesService(FilesService filesService) {
		this.filesService = filesService;
	}

	public UserService getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public AnswersService getAnswersService() {
		return answersService;
	}
	@Autowired
	public void setAnswersService(AnswersService answersService) {
		this.answersService = answersService;
	}

	public LecturesService getLecturesService() {
		return lecturesService;
	}
	@Autowired
	public void setLecturesService(LecturesService lecturesService) {
		this.lecturesService = lecturesService;
	}

	public ClientService getClientService() {
		return clientService;
	}
	@Autowired
	public void setClientService(ClientService clientService) {
		this.clientService = clientService;
	}

	public QuestionTypeService getQuestionTypeService() {
		return questionTypeService;
	}
	@Autowired
	public void setQuestionTypeService(QuestionTypeService questionTypeService) {
		this.questionTypeService = questionTypeService;
	}

}
